/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.dao;

import org.ipti.br.tag.entity.Campaign;
import org.ipti.br.tag.entity.Campaign_;
import org.ipti.br.tag.entity.enums.CampaignType;
import java.util.Calendar;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
/**
 *
 * @author FelipeMSX
 */
@Stateless
public class CampaignFacade extends AbstractFacade<Campaign> {

    @PersistenceContext(unitName = "TAG2PU")
    private EntityManager em;
     
    public CampaignFacade() {
        super(Campaign.class);
    }
    
    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    @Override
    public void create(Campaign Campaign ) {
        getEntityManager().persist(Campaign );
    }

    @Override
    public void edit(Campaign  Campaign ) {  
        getEntityManager().merge(Campaign);
    }
    
    public void editForSynchronization(Campaign Campaign ) {
        getEntityManager().merge(Campaign);
    }
    
    /*
     * Trazer todas as campanhas pelo tipo
     */
    public List<Campaign> findAllCampaign(CampaignType campaignType) {
      
        CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
        CriteriaQuery<Campaign> cq = cb.createQuery(Campaign.class);
        Root<Campaign> root = cq.from(Campaign.class);
        
        cq.where(cb.equal(root.get(Campaign_.type), campaignType) );
               
        try {
            return getEntityManager().createQuery(cq).getResultList();
        } catch (NoResultException e) {
            e.printStackTrace();
            return null;
        }
        
    }
    
    public Campaign findLastCampaing(CampaignType campaignType) {
        CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
        CriteriaQuery<Campaign> cq = cb.createQuery(Campaign.class);
        Root<Campaign> root = cq.from(Campaign.class);
        
        Predicate predicate1 = cb.equal( root.get(Campaign_.type), campaignType);
        cq.where(predicate1);
        
        cq.orderBy(cb.desc(root.get(Campaign_.initialDate)));
        try {
            TypedQuery<Campaign> q = getEntityManager().createQuery(cq);
            q.setMaxResults(1);
            return q.getResultList().get(0);
        } catch (NoResultException e) {
            e.printStackTrace();
            return null;
        } catch (ArrayIndexOutOfBoundsException e) {
            e.printStackTrace();
            return null;
        }
    }
    
//    public List<Object[]> getCampaignListHb(Unity unity, Campaign campaign) {
//        CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
//        CriteriaQuery<Object[]> cq = cb.createQuery(Object[].class);
//        Root<Hb> rt = cq.from(Hb.class);
//        
//        Predicate p1 = cb.equal(rt.join(Hb_.campaign).get(Campaign_.id), campaign.getId());
//        Predicate p2 = cb.equal(rt.join(Hb_.human).join(Human_.actors).join(Actor_.unity).get(Unity_.id), unity.getId());
//        
//        cq.multiselect(rt.join(Hb_.human), rt);
//        cq.where(p1,p2);
//        
//        cq.orderBy(cb.desc(rt.get(Hb_.creationDate)));
//        cq.distinct(true);
//        try{
//            return getEntityManager().createQuery(cq).getResultList();
//            
//        } catch(NoResultException e) {
//            e.printStackTrace();
//            return null;
//        }
//        
//    }
    
//    public List<Object[]> getCampaignListWeigthAndHeight(Unity unity, Campaign campaign) {
//        CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
//        CriteriaQuery<Object[]> cq = cb.createQuery(Object[].class);
//        Root <Human> root = cq.from(Human.class);
//        
//        Predicate predicate1 = cb.equal(root.join(Human_.weightAndHeight).join(WeightAndHeight_.campaign).get(Campaign_.id), campaign.getId());
//        Predicate predicate2 = cb.equal( root.join(Human_.actors).join(Actor_.unity).get(Unity_.id), unity.getId());
//        Predicate predicate3 = cb.notEqual( root.join( Human_.weightAndHeight).get(WeightAndHeight_.weight), 0.0);
//        Predicate predicate4 = cb.notEqual( root.join( Human_.weightAndHeight).get(WeightAndHeight_.height), 0.0);
//
//        cq.multiselect( root.join(Human_.weightAndHeight).get(WeightAndHeight_.human), root.get(Human_.weightAndHeight) ).where( predicate1,
//                predicate2, predicate3, predicate4).distinct(true);
//        
//        cq.orderBy( cb.asc(root.get(Human_.firstName)), cb.asc(root.get(Human_.lastName) ));
//        try{
//            return getEntityManager().createQuery(cq).getResultList();
//            
//        }catch(NoResultException e){
//            e.printStackTrace();
//            return null;
//        }
//    }
          
//    public List<Human> getx(Unity unity, Campaign campaign){
//        CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
//        CriteriaQuery<Human> cq = cb.createQuery(Human.class);
//        Root <Human> root = cq.from(Human.class);  
//
//
//        Predicate predicate23 = cb.equal(root.join(Human_.weightAndHeight).join(WeightAndHeight_.campaign).get(Campaign_.id), campaign.getId());
//////            Predicate predicate2 = cb.equal( root.join(Human_.actors, ).join(Actor_.unity, JoinType.INNER).get(Unity_.id), unity.getId());
////            Predicate predicate3 = cb.notEqual( root.join( Human_.weightAndHeight).get(WeightAndHeight_.weight), 0.0);
////            Predicate predicate4 = cb.notEqual( root.join( Human_.weightAndHeight).get(WeightAndHeight_.height), 0.0);
//        Predicate predicate1 = cb.equal(root.join(Human_.actors).join(Actor_.unity).get(Unity_.id), unity.getId());
//
//        cq.where( cb.and(predicate1, predicate23));
//
//        cq.orderBy( cb.asc(root.get(Human_.firstName)), cb.asc(root.get(Human_.lastName) ));
//        try{
//            return getEntityManager().createQuery(cq).getResultList();
//
//        } catch (NoResultException e) {
//            e.printStackTrace();
//            return null;
//        }
//    }
    
    public List<Campaign> modifiedPerfFreqBetweenDate(Calendar initialDate, Calendar finalDate, Long serialNumber) {
        CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
        CriteriaQuery<Campaign> cq = cb.createQuery(Campaign.class);
        Root<Campaign> from = cq.from(Campaign.class);
        Predicate predicate1 = cb.notEqual(from.get(Campaign_.serialNumber), serialNumber);
        Predicate predicate2 = cb.greaterThanOrEqualTo(from.get(Campaign_.lastModified), initialDate);
        Predicate predicate3 = cb.lessThan(from.get(Campaign_.lastModified), finalDate);
        cq.where(cb.and(predicate1, predicate2, predicate3));

        return getEntityManager().createQuery(cq).getResultList();
    }
}
